package com.ruoyi.miniapp.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.miniapp.domain.MiniActivitySignup;
import com.ruoyi.miniapp.domain.MiniChildInfo;
import com.ruoyi.miniapp.domain.MiniGroupInfo;
import com.ruoyi.miniapp.domain.vo.MiniActivityVO;
import com.ruoyi.miniapp.mapper.MiniActivitySignupMapper;
import com.ruoyi.miniapp.mapper.MiniChildInfoMapper;
import com.ruoyi.miniapp.mapper.MiniGroupInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.miniapp.mapper.MiniActivityMapper;
import com.ruoyi.miniapp.domain.MiniActivity;
import com.ruoyi.miniapp.service.IMiniActivityService;
import org.springframework.util.ObjectUtils;

/**
 * 活动Service业务层处理
 * 
 * @author duantao
 * @date 2025-09-09
 */
@Service
public class MiniActivityServiceImpl implements IMiniActivityService 
{
    @Autowired
    private MiniActivityMapper miniActivityMapper;
    @Autowired
    private MiniActivitySignupMapper miniActivitySignupMapper;
    @Autowired
    private MiniChildInfoMapper  childInfoMapper;
    @Autowired
    private MiniGroupInfoMapper groupInfoMapper;


    /**
     * 获取活动剩余名额
     * @param activityId 活动ID
     * @return 剩余名额
     */
    public int getActivityLeavings(Long activityId){
        MiniActivity miniActivity = miniActivityMapper.selectMiniActivityByActivityId(activityId);
        Integer limit = miniActivity.getActivityLimit();
        // 为空代表不限制人数
        if (ObjectUtils.isEmpty(limit)){
            return -1;
        }

        MiniActivitySignup selectParam = new MiniActivitySignup();
        selectParam.setActivityId(activityId);
        List<MiniActivitySignup> miniActivitySignups = miniActivitySignupMapper.selectMiniActivitySignupList(selectParam);
        // 已报名人数
        int signUpNum = 0;
        for (MiniActivitySignup miniActivitySignup : miniActivitySignups) {
            if ("0".equals(miniActivitySignup.getSignUpType())) {
                signUpNum += 1;
            } else {
                if (!ObjectUtils.isEmpty(miniActivitySignup.getGroupPersonNum())) {
                    signUpNum += miniActivitySignup.getGroupPersonNum();
                }
            }
        }
        // 剩余名额 = 人数限制-已报名人数
        return limit - signUpNum;
    }


    /**
     * 查询活动
     * 
     * @param activityId 活动主键
     * @return 活动
     */
    @Override
    public MiniActivityVO selectMiniActivityByActivityId(Long activityId)
    {
        MiniActivity miniActivity = miniActivityMapper.selectMiniActivityByActivityId(activityId);
        MiniActivityVO  miniActivityVO = new MiniActivityVO();
        BeanUtils.copyProperties(miniActivity,miniActivityVO);

        miniActivityVO.setActivityLeavings(getActivityLeavings(activityId));
        return miniActivityVO;
    }

    /**
     * 查询活动列表
     * 
     * @param miniActivity 活动
     * @return 活动
     */
    @Override
    public List<MiniActivityVO> selectMiniActivityList(MiniActivity miniActivity)
    {
        List<MiniActivity> miniActivities = miniActivityMapper.selectMiniActivityList(miniActivity);
        List<MiniActivityVO> miniActivityVOList = new ArrayList<>();


        for (MiniActivity activity : miniActivities) {
            MiniActivityVO miniActivityVO = new MiniActivityVO();
            BeanUtils.copyProperties(activity,miniActivityVO);
            miniActivityVO.setActivityLeavings(getActivityLeavings(activity.getActivityId()));
            miniActivityVOList.add(miniActivityVO);


        }
        return miniActivityVOList ;
    }

    /**
     * 新增活动
     * 
     * @param miniActivity 活动
     * @return 结果
     */
    @Override
    public int insertMiniActivity(MiniActivity miniActivity)
    {
        miniActivity.setCreateTime(DateUtils.getNowDate());
        return miniActivityMapper.insertMiniActivity(miniActivity);
    }

    /**
     * 修改活动
     * 
     * @param miniActivity 活动
     * @return 结果
     */
    @Override
    public int updateMiniActivity(MiniActivity miniActivity)
    {
        miniActivity.setUpdateTime(DateUtils.getNowDate());
        return miniActivityMapper.updateMiniActivity(miniActivity);
    }

    /**
     * 批量删除活动
     * 
     * @param activityIds 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteMiniActivityByActivityIds(Long[] activityIds)
    {
        return miniActivityMapper.deleteMiniActivityByActivityIds(activityIds);
    }

    /**
     * 删除活动信息
     * 
     * @param activityId 活动主键
     * @return 结果
     */
    @Override
    public int deleteMiniActivityByActivityId(Long activityId)
    {
        return miniActivityMapper.deleteMiniActivityByActivityId(activityId);
    }

    @Override
    public List<MiniChildInfo> getSignUpInfoByChildren(Long activityId) {
        List<MiniChildInfo> childInfos = new ArrayList<>();
        MiniActivitySignup selectParam = new MiniActivitySignup();
        selectParam.setActivityId(activityId);
        selectParam.setSignUpType("0");
        List<MiniActivitySignup> miniActivitySignups = miniActivitySignupMapper.selectMiniActivitySignupList(selectParam);
        if (ObjectUtils.isEmpty(miniActivitySignups)) {
            return childInfos;
        }
        List<Long> childrenIds = new ArrayList<>();
        for (MiniActivitySignup miniActivitySignup : miniActivitySignups) {
            Long childrenId = miniActivitySignup.getChildrenId();
            childrenIds.add(childrenId);
        }
        childInfos =  childInfoMapper.selectMiniChildInfoListByIds(childrenIds);

        return childInfos;
    }

    @Override
    public List<MiniGroupInfo> getSignUpInfoByGroup(Long activityId) {
        List<MiniGroupInfo> groupInfos = new ArrayList<>();
        MiniActivitySignup selectParam = new MiniActivitySignup();
        selectParam.setActivityId(activityId);
        selectParam.setSignUpType("1");
        List<MiniActivitySignup> miniActivitySignups = miniActivitySignupMapper.selectMiniActivitySignupList(selectParam);

        if (ObjectUtils.isEmpty(miniActivitySignups)) {
            return groupInfos;
        }
        List<Long> groupIds = new ArrayList<>();
        for (MiniActivitySignup miniActivitySignup : miniActivitySignups) {
            Long groupId = miniActivitySignup.getGroupId();
            groupIds.add(groupId);
        }

        groupInfos =  groupInfoMapper.selectMiniGroupInfoByGroupIds(groupIds);

        return groupInfos;
    }
}
